<?php


namespace App\Http\Controllers\App;


use App\Exceptions\FailException;
use App\Http\Controllers\Controller;
use App\Models\Feedback;
use Carbon\Carbon;
use Auth;

class FeedbackController extends Controller
{
    public function store()
    {
        $this->validate(request(), [
            'content' => 'required',
            'phone' => ['required', 'regex:/^1[345789][0-9]{9}$/']
        ], [
            'content.required' => '内容不能为空',
            'phone.regex' => '手机号格式错误'
        ]);


        $feedback = new Feedback();
        if (Auth::check()) {
            $feedback->member_id = Auth::id();
        }
        $feedback->content = request('content');
        $feedback->phone = request('phone');
        $feedback->created_at = Carbon::now();
        if (!$feedback->save()) {
            throw new FailException('反馈失败');
        }
        return success('反馈成功');
    }
}